// -----------------------------------------------------------------------
// <copyright file="UserDepartmentEN.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Class UserDepartment
    /// </summary>
    public class UserDepartmentEN : EntityEN
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserDepartmentEN" /> class
        /// </summary>
        public UserDepartmentEN()
        {
            this.UserDepartmentId = 0;
            this.UserId = 0;
            this.DepartmentId = 0;
            this.PositionsId = 0;
            this.Active = true;
        }

        /// <summary>
        /// Gets or sets UserDepartmentId
        /// </summary>
        public int UserDepartmentId { get; set; }

        /// <summary>
        /// Gets or sets UserId
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// Gets or sets DepartmentId
        /// </summary>
        public int DepartmentId { get; set; }

        /// <summary>
        /// Gets or sets PositionsId
        /// </summary>
        public int PositionsId { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether Active
        /// </summary>
        public bool Active { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="UserDepartmentEN"/> is default.
        /// </summary>
        /// <value>
        ///   <c>true</c> if default; otherwise, <c>false</c>.
        /// </value>
        public bool Default { get; set; }

        /// <summary>
        /// Gets Department
        /// </summary>
        public DepartmentEN Department
        {
            get
            {
                try
                {
                    DepartmentEN department = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDepartmentGetById, this.DepartmentId);

                    while (reader.Read())
                    {
                        department = new DepartmentEN();

                        if (!reader.IsDBNull(0))
                        {
                            department.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            department.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            department.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            department.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            department.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return department;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets Position
        /// </summary>
        public PositionEN Position
        {
            get
            {
                try
                {
                    PositionEN positions = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOnePositionsGetById, this.PositionsId);
                    while (reader.Read())
                    {
                        positions = new PositionEN();

                        if (!reader.IsDBNull(0))
                        {
                            positions.PositionsId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            positions.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            positions.Name = reader.GetString(2);
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                    return positions;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }   
            }
        }

        /// <summary>
        /// Gets User
        /// </summary>
        public UserEN User
        {
            get
            {
                try
                {
                    UserEN user = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUsersGetById, this.UserId);

                    while (reader.Read())
                    {
                        user = new UserEN();

                        if (!reader.IsDBNull(0))
                        {
                            user.UserId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            user.AdministrativeUnitsId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            user.UserName = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            user.FullName = reader.GetString(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            user.Sex = reader.GetBoolean(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            user.BirthDate = reader.GetDateTime(5);
                        }

                        if (!reader.IsDBNull(6))
                        {
                            user.Email = reader.GetString(6);
                        }

                        if (!reader.IsDBNull(7))
                        {
                            user.Sms = reader.GetString(7);
                        }

                        if (!reader.IsDBNull(8))
                        {
                            user.Phone = reader.GetString(8);
                        }

                        if (!reader.IsDBNull(9))
                        {
                            user.Address = reader.GetString(9);
                        }

                        if (!reader.IsDBNull(10))
                        {
                            user.CreateDate = reader.GetDateTime(10);
                        }

                        if (!reader.IsDBNull(11))
                        {
                            user.Send = reader.GetString(11);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return user;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets the ASP net users in roles.
        /// </summary>
        /// <value>
        /// The ASP net users in roles.
        /// </value>
        /// <exception cref="System.Exception">
        /// the exc</exception>
        public IList<AspNetUsersInRolesEN> AspNetUsersInRoles
        {
            get
            {
                try
                {
                    IList<AspNetUsersInRolesEN> lstUserDepartment = new List<AspNetUsersInRolesEN>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAspNetUsersInRolesByUserDepartmentId, this.UserDepartmentId);
                    while (reader.Read())
                    {
                        AspNetUsersInRolesEN aspNetUsersInRoles = new AspNetUsersInRolesEN();

                        if (!reader.IsDBNull(0))
                        {
                            aspNetUsersInRoles.UserId = reader.GetGuid(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            aspNetUsersInRoles.RoleId = reader.GetGuid(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            aspNetUsersInRoles.UserDepartmentId = reader.GetInt32(2);
                        }

                        lstUserDepartment.Add(aspNetUsersInRoles);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstUserDepartment;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
